package com.tj.jinzao.presenter;


import android.util.Log;

import com.tj.jinzao.api.SubscriberCallBack;
import com.tj.jinzao.base.BasePresenter;
import com.tj.jinzao.callback.ISignView;
import com.tj.jinzao.manager.UserManager;
import com.tj.jinzao.model.Mission;
import com.tj.jinzao.model.ResultResponse;
import com.tj.jinzao.model.Sign;
import com.tj.jinzao.model.User;
import com.tj.jinzao.utils.Utils;

import java.util.Calendar;
import java.util.List;

/**
 * 地址处理
 * Created by wjy 2018/12/10.
 */
public class SignPresenter extends BasePresenter<ISignView<List<Sign>>> {

    public SignPresenter(ISignView<List<Sign>> view) {
        super(view);
    }

    public void getSignList(final boolean first) {
        long uId = UserManager.getInstance().getUser().getId();
        int year = Calendar.getInstance().get(Calendar.YEAR);
        int month = Calendar.getInstance().get(Calendar.MONTH) + 1;
        String time = year + "-" + ((month < 10) ? ("0" + month) : month);
        addSubscription(mApiService.getSignList(uId, time)
                , new SubscriberCallBack<List<Sign>>(mView.getContext()) {
                    @Override
                    protected void onSuccess(List<Sign> list) {
                        Calendar calendar = Calendar.getInstance();
                        int year = calendar.get(Calendar.YEAR);
                        int month = calendar.get(Calendar.MONTH) + 1;
                        int day = calendar.get(Calendar.DAY_OF_MONTH);
                        boolean hasSign = false;
                        for (Sign sign : list) {
                            try {
                                String dates[] = sign.getCheckDay().split("-");
                                if (Utils.parseInt(dates[0]) == year
                                        && Utils.parseInt(dates[1]) == month
                                        && Utils.parseInt(dates[2]) == day) {
                                    hasSign = true;
                                    break;
                                }
                            } catch (Exception e) {
                            }
                        }
                        if (first && !hasSign) {
                            sign(year + "-" + (month < 10 ? ("0" + month) : month) + "-" + (day < 10 ? ("0" + day) : day));
                        } else {
                            if (mView != null) mView.onRequestSuccess(list);
                        }
                    }

                    @Override
                    protected void onFailure(ResultResponse response) {
                        if (mView != null) mView.onRequestError(response.getMessage());
                    }

                });
    }

    public void sign(String date) {
        long uId = UserManager.getInstance().getUser().getId();
        addSubscription(mApiService.sign(uId, date)
                , new SubscriberCallBack<Sign>(mView.getContext()) {
                    @Override
                    protected void onSuccess(Sign list) {
                        if (mView != null) mView.onSignSuccess();
                        getUserInfo();
                    }

                    @Override
                    protected void onFailure(ResultResponse response) {
                        if (mView != null) mView.onRequestError(response.getMessage());
                    }

                });
    }

    public void getMission() {
        long uId = UserManager.getInstance().getUser().getId();
        Log.w("数据而技术",""+uId);

        addSubscription(mApiService.getMissionList(uId)
                , new SubscriberCallBack<List<Mission>>(mView.getContext()) {
                    @Override
                    protected void onSuccess(List<Mission> list) {
                        if (mView != null) mView.onGetMission(list);
                    }

                    @Override
                    protected void onFailure(ResultResponse response) {
                        if (mView != null) mView.onRequestError(response.getMessage());
                    }

                });
    }

    public void getUserInfo() {
        long uId = UserManager.getInstance().getUser().getId();
        addSubscription(mApiService.getUserInfo(uId)
                , new SubscriberCallBack<User>(mView.getContext()) {
                    @Override
                    protected void onSuccess(User user) {
                        UserManager.saveUserData(user);
                        getSignList(false);
                    }

                    @Override
                    protected void onFailure(ResultResponse response) {
                        if (mView != null) mView.onRequestError(response.getMessage());
                    }

                });
    }


}